/* 
 * Advanced Foundation Classes
 * Copyright (C) 2000/2004  Fabio Rotondo - Andrea Galimberti
 *  
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *  
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *  
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 */
#include "optmenu.h"

static const char class_name [] = "EGUI Widget OptMenu";

static int afc_easygui_internal_optmenu_get_template ( DynamicClass * dyn );
static int afc_easygui_internal_cb_optmenu_start ( DynamicClass * dyn );
static int afc_easygui_internal_cb_optmenu_end ( DynamicClass * dyn );

static int afc_easygui_optmenu_method_add ( DynamicClass * dyn );
static int afc_easygui_optmenu_method_set ( DynamicClass * dyn );
static int afc_easygui_optmenu_method_get ( DynamicClass * dyn );


static void afc_easygui_internal_optmenu_event_changed ( GtkOptionMenu * menu, DynamicClass * dyn );

DynamicClass * afc_easygui_internal_optmenu_new_instance ()
{
	DynamicClass * dyn = NULL;

	if ( ( dyn = afc_dynamic_class_new () ) == NULL ) 
	{
		AFC_LOG_FAST ( AFC_ERR_NO_MEMORY );
		return ( NULL );
	}

	afc_dynamic_class_add_method ( dyn, "get_template", NULL,  afc_easygui_internal_optmenu_get_template );
	afc_dynamic_class_add_method ( dyn, "open_callback", NULL,  afc_easygui_internal_cb_optmenu_start );
	afc_dynamic_class_add_method ( dyn, "close_callback", NULL,  afc_easygui_internal_cb_optmenu_end );
	afc_dynamic_class_add_method ( dyn, "enabled",	NULL, 	afc_easygui_widget_method_enabled );

	afc_dynamic_class_add_method ( dyn, "add", NULL,  afc_easygui_optmenu_method_add );
	afc_dynamic_class_add_method ( dyn, "set", NULL,  afc_easygui_optmenu_method_set );
	afc_dynamic_class_add_method ( dyn, "get", NULL,  afc_easygui_optmenu_method_get );

	return ( dyn );
}

int afc_easygui_internal_optmenu_del_instance ( DynamicClass * dyn )
{
	return ( afc_easygui_widget_standard_free ( dyn ) );
}

static int afc_easygui_internal_optmenu_get_template ( DynamicClass * dyn )
{
	dyn->result = "CHANGED/K";
	return ( AFC_ERR_NO_ERROR );
}

static int afc_easygui_internal_cb_optmenu_start ( DynamicClass * dyn )
{
	EasyGUI * egui = dyn->info;
	EasyGUIContext * context = egui->curr_context;
	GtkWidget * w;
	GtkWidget * menu;
	DynamicClass * dc;
	char * changed;

	if ( context->curr_container == NULL ) return ( AFC_EASYGUI_ERR_NO_CONTAINER );

	afc_easygui_widget_parse_default_params ( egui );

	if ( ( dc = afc_dynamic_class_master_new_instance ( egui->commands, "optmenu" ) ) == NULL )
		return ( AFC_LOG_FAST_INFO ( AFC_ERR_NO_MEMORY, "dynclass instance" ) );

	// afc_dprintf ( "%s: context: %p %p\n", __FUNCTION__, dc, context );

	WIDGET_SET_CONTEXT ( dc, context );

	// Create the Option Menu
	w = gtk_option_menu_new ();

	// And the real menu that will be appended to it
	menu = gtk_menu_new ();

	afc_dynamic_class_set_var ( dc, AFC_DYNAMIC_CLASS_VAR_KIND_POINTER, "menu", menu );

	// Set the menu to the Option Menu
	gtk_option_menu_set_menu ( GTK_OPTION_MENU ( w ), menu );

	// Get the "changed" event
	changed	 = afc_cmd_parser_arg_get_by_name ( egui->parser, "CHANGED" );

	// if the user has specified something, we link the event
	if ( changed != NULL ) 
	{
		g_signal_connect ( G_OBJECT ( w ), "changed", G_CALLBACK ( afc_easygui_internal_optmenu_event_changed ), dc );
		afc_easygui_widget_store_function ( context, dc, "changed", changed );
	}

	return ( afc_easygui_context_add_container ( context, w, dc, AFC_EASYGUI_CONTAINER_TYPE_OPTMENU, AFC_EASYGUI_CONTAINER_HOMOGENEOUS_NONE ) );
}

static int afc_easygui_internal_cb_optmenu_end ( DynamicClass * dyn )
{
	EasyGUI * egui = dyn->info;
	EasyGUIContext * context = egui->curr_context;

	gtk_option_menu_set_history ( GTK_OPTION_MENU ( context->curr_container->gtk_handle ), 0 );
	
	return ( afc_easygui_context_close_container ( context ) );
}

// METHODS
static int afc_easygui_optmenu_method_add ( DynamicClass * dyn )
{
	GtkWidget * w;
	GtkWidget * menu;

	w = afc_array_master_first ( dyn->args );

	// Gets the menu
	menu = afc_dynamic_class_get_var ( dyn, "menu" );

	gtk_menu_shell_append ( GTK_MENU_SHELL ( menu ), w );

	return ( AFC_ERR_NO_ERROR );
}

// set ( val )
static int afc_easygui_optmenu_method_set ( DynamicClass * dyn )
{
	EasyGUIWidgetData * ewidget;
	int v;

	v = ( int )  afc_array_master_first ( dyn->args );

	WIDGET_GET_EWIDGET ( dyn, ewidget );

	gtk_option_menu_set_history ( GTK_OPTION_MENU ( ewidget->gtk_handle ), v );

	return ( AFC_ERR_NO_ERROR );
}

// get ()  -> result in dyn->result
static int afc_easygui_optmenu_method_get ( DynamicClass * dyn )
{
	EasyGUIWidgetData * ewidget;

	WIDGET_GET_EWIDGET ( dyn, ewidget );

	dyn->result = ( void * ) gtk_option_menu_get_history ( GTK_OPTION_MENU ( ewidget->gtk_handle ) );
	return ( AFC_ERR_NO_ERROR );
}

// EVENTS
static void afc_easygui_internal_optmenu_event_changed ( GtkOptionMenu * menu, DynamicClass * dc )
{
	EasyGUIOptMenuSignalFunc sig;
	int i, last;
	// EasyGUIContext * context = WIDGET_GET_CONTEXT ( dc );

	// We avoid raising the event when the user selects the same popup value twice in a row
	i 	= gtk_option_menu_get_history ( GTK_OPTION_MENU ( menu ) );
	last 	= ( int ) afc_dynamic_class_get_var ( dc, "egui_last_val" );

	if ( i == last ) return;

	afc_dynamic_class_set_var ( dc, AFC_DYNAMIC_CLASS_VAR_KIND_NUM, "egui_last_val", ( void * ) i );
	
	// afc_dprintf ( "%s: context: %p %p\n", __FUNCTION__, dc, context );

	if ( dc->private_vars ) \
	  if ( ( sig = ( EasyGUIOptMenuSignalFunc ) afc_dictionary_get ( dc->private_vars, "changed" ) ) != NULL )  
		sig ( dc, WIDGET_GET_CONTEXT ( dc ), i );
} 
